package cn.boy.gui.adminstrator;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.ProgressDialog;
import android.app.DatePickerDialog.OnDateSetListener;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;
import cn.boy.adapter.RankAdapter;
import cn.boy.domain.Dormitory;
import cn.boy.home.R;
import cn.boy.service.HomeService;

public class RankActivity extends Activity implements OnDateSetListener{
	
	protected static final String TAG = "RankActivity";
	EditText txtFloor;
	Button dateButton;
	ListView listView;
	
	List<Dormitory> dorms;
	
	ProgressDialog pd;
	String retValue;
	
	int mYear ;
	int mMonth;
	int mDay;
	
	RankAdapter adapter;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.rank);
		
		findViews();
		
		dorms = new ArrayList<Dormitory>();
		
		
		setDefaultDate();
		
		dateButton.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				showDatePicker();
			}
		});
	}

	private void findViews() {

		txtFloor = (EditText)findViewById(R.id.et_floor_no);
		dateButton = (Button)findViewById(R.id.btn_date);
		listView = (ListView)findViewById(R.id.lv_rank);
	}

	private void setDefaultDate() {
		Calendar c = Calendar.getInstance();
		mYear = c.get(Calendar.YEAR);
		mMonth = c.get(Calendar.MONTH);
		mDay = c.get(Calendar.DAY_OF_MONTH);
	}
	private void showDatePicker() {
		DatePickerDialog dpd = new DatePickerDialog(this, this, mYear, mMonth, mDay);
		dpd.show();
	}

	public void onDateSet(DatePicker view, int year, int monthOfYear,
			int dayOfMonth) {
			
		dateButton.setText(year + "-" + (monthOfYear + 1) + "-"+ dayOfMonth);
		
		final String prex = txtFloor.getText().toString().trim();
		final String date = dateButton.getText().toString().trim();
		
		if(!TextUtils.isEmpty(prex) && ! TextUtils.isEmpty(date)){
			showDialog();
			new Thread(new Runnable() {
				public void run() {
					try {
						retValue = HomeService.rankDormitory(prex, date);
						Log.i(TAG, "retValue---->>" + retValue);
						if(!retValue.equals("")){
							sendHandlerMessage(1);
						}else{
							sendHandlerMessage(2);
						}
					} catch (Exception e) {
						Log.e(TAG, e.toString());
						sendHandlerMessage(0);
					}
				}
			}).start();
		}else{
			Toast.makeText(RankActivity.this, R.string.floor_no_and_date, 1).show();
		}
		
		
	}
	
	private void showDialog() {
		pd = new ProgressDialog(RankActivity.this); 
		pd.setMessage(getResources().getString(R.string.get_dorms_ing));
		pd.show();
	}
	
	private void sendHandlerMessage(int what) {
		Message msg = handler.obtainMessage(what);
		handler.sendMessage(msg);
		
	}
	
	Handler handler = new Handler(){
		public void handleMessage(android.os.Message msg) {
			if(pd!=null){
				pd.dismiss();
			}
			switch(msg.what){
			case 0:
				Toast.makeText(RankActivity.this, R.string.net_unreacgabke, 1).show();
				break; 
			case 1:
				try {
					dorms.clear();
					JSONArray array = new JSONArray(retValue);
					for(int i = 0;i < array.length();i++){
						JSONObject obj = array.getJSONObject(i);
						Dormitory dorm = mappingEntity(obj);
						dorms.add(dorm);
					}
					if(adapter == null){
						adapter = new RankAdapter(RankActivity.this, dorms);
						listView.setAdapter(adapter);
					}else{
						adapter.setDorms(dorms);
						adapter.notifyDataSetChanged();
					}
				} catch (JSONException e) {
					Log.e(TAG, e.toString());
				}
				break;
			case 2:
				Toast.makeText(RankActivity.this, R.string.get_data_fail, 1).show();
				break;
			}
		}

		
	};

	private Dormitory mappingEntity(JSONObject obj) {
		
		try {
			String no = obj.getString("no");
			String name = obj.getString("name");
			String mark = obj.getString("mark");
			String date = obj.getString("date");
			Dormitory dorm = new Dormitory(no, name, mark, date);
			return dorm;
		} catch (JSONException e) {
			Log.e(TAG, e.toString());
			return null;
		}
		
	};
}
